#include <stdio.h>
#include<stdlib.h>
int n=-1;
int *a;

int append (int num)
{
	int i;
	n++;
	a=realloc(a,(n+1)*sizeof(int*));		
	a[n]=num;
	
	return n;
}

int extend(int b[100],int k)
{
	int i;
	a=realloc(a,(n+i)*sizeof(int*));
	for(i=0;i<k;i++)
	{ 
		n++;
		a[n]=b[i];
		
	}
	return n;
}

int insert(int c,int d)
{
		
	int k,temp,i;
	n++;
	a = realloc(a,(n+1)*sizeof(int*)); 
	temp=a[c-1];
	a[c-1]=d;
	for(k=n;k>=c;k--)
		{	
			a[k+1]=a[k];
			
		}
	a[c]=temp;
	
	return n;
}

int remove1(int e)
{
	int j,k,flag=0;
	for(j=0;j<=n;j++)
		{
			if(a[j]==e)
			{
			flag=1;
				for(k=j;k<n;k++)
				{
					a[k]=a[k+1];
				}
			break;	
			}
		}
	if (flag ==0)
	printf ("element not found ");
	n--;
	return n;	
}

int pop (int f)
{
	int value,j;
	value=a[f-1];
	for(j=f+1;j<=n;j++)
	{
		a[j-1]=a[j];
		
	}
	n--;
	return value;
	return n;
}

int index1(int h)
{


	int j,flag=0;
	for(j=0;j<=n;j++)
	{
	
		if(a[j]==h)
		{
			flag =1;
			printf(" %d",j+1);
		}
	}
	if(flag==0)
	printf("element not found");
	return j+1;
}

void sort()
{
int i,j,temp;

for(i=0;i<=n;i++)
	{
		for(j=0;j<=n;j++)
		{
			if(a[j]>a[i])
			{
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
		}
	}
}

int count(int p)
{
	int c=0,counter=0,j;
		for(j=0;j<=n;j++)
		{
			counter=1;
			if(a[j]==p)
			{
				c++;
			}
		}
	if(counter==0)
	printf("element not found");
		
return c;
}

void display()
{
int j;
printf(" %d",n);
for (j=0;j<=n;j++)
{
printf("Element at position %d is %d \n",(j+1),a[j]);
}
}



void reverse()
{
int j,temp,q;
q=n;
for (j=0;j<=n/2;j++)
{
			temp=a[j]; 
			a[j]=a[q];
			a[q]=temp;
			q--;
}
}


void main()
	{
	int i;
	while(1)  
	{
		printf("1.To append the list \n2. To extend the list\n3. To insert an element in list \n4.To remove an element from a list\n5.To pop an element from list \n6. To return the index1 of number in the list\n7. To sort the list \n8.To reverse the list \n9.To count the number of occurence of a number in list \n10.To display the list \n0. To exit\n");
		scanf(" %d",&i);
	switch(i)
	{
		case 1:
		{
			int num;
			printf("Enter the number u want to append :");
			scanf(" %d",&num);
			append(num);
			break;
		}
	
		case 2:
		{
			int b[100],k;
			printf("Enter the value of k i.e. number of elements in an array: ");
			scanf("%d",&k);
			for (i=0;i<k;i++)
			{
				scanf("%d",&b[i]);
			}
			extend(b,k);
			break;
		}

		case 3:
	        {
			int c,d;
			printf("Enter the number you want to insert :");
			scanf(" %d",&d);
			printf("Enter the position at which you want to insert :");
			scanf(" %d",&c);
			insert(c,d);
			break;
	        }
		case 4:
                {
			int e;
			printf("Enter the number you want to delete:");
			scanf(" %d",&e);
			remove1(e);
			break;
	 	}
		case 5:
          	{
			int f,g;
			printf("Enter the position at which you want to delete or 0 if you don't want to specify the position :");
			scanf(" %d",&f);
			if (f==0)
			g=pop(n-1);
			else
			g=pop(f-1);
			printf("Element being deleted is %d",g);
			break;
	        }
		case 6:
		{
			int h,m;
			printf("Enter the value whose index you want to know: ");
			scanf("%d",&h);
			index1(h);
			break;
		}
		case 7:
		{
			sort ();
			break;
		}
		case 8:
		{
			reverse();
			break;
		}
		case 9:
         	{
			int x,o;
			printf("Enter the element whose count you want to find : ");
			scanf(" %d",&o);
			x=count(o);
			printf("Element count is %d ",x);
			break;
	 	}
		case 10:
		{
			display();
			break;
		}
		case 0:
		{
		exit(0);
		}
				
	}
	}
}

